package com.eastedu.report.principal.assemble;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.*;
import com.deepoove.poi.util.PoitlIOUtils;
import com.eastedu.report.common.model.parameter.ReportGenerateParameter;
import com.eastedu.report.common.model.po.CooperativeTeacherPO;
import com.eastedu.report.common.util.SqlUtil;
import com.eastedu.report.mapper.CooperativeTeacherMapper;
import com.eastedu.utils.CollectionUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 标题数据组装
 *
 * @author superman
 */
@Service
public class TitleReportDataAssembly implements ReportDataAssembly {

    private final CooperativeTeacherMapper cooperativeTeacherMapper;

    /**
     * Instantiates a new Title report data assembly.
     *
     * @param cooperativeTeacherMapper the digital resource platform cooperative teacher mapper
     */
    public TitleReportDataAssembly(CooperativeTeacherMapper cooperativeTeacherMapper) {
        this.cooperativeTeacherMapper = cooperativeTeacherMapper;
    }

    @Override
    public Map<String, Object> assemble(ReportGenerateParameter parameter, List<String> schoolPeriods, List<String> subjects) {
        LambdaQueryWrapper<CooperativeTeacherPO> queryWrapper = SqlUtil.queryWrapper(parameter, CooperativeTeacherPO.class);
        Map<String, Object> map = Maps.newHashMap();


        List<CooperativeTeacherPO> pos = cooperativeTeacherMapper.selectList(queryWrapper);
        map.putAll(pos.stream()
                           .findFirst()
                           .map(po -> {
                                    Integer resourceSubjectTeacherNumber = po.getResourceSubjectTeacherNumber();
                                    Integer resourceSecretTeacherNumber = po.getResourceSecretTeacherNumber();

                                    Map<String, Object> part = Maps.newHashMap();
                                    List<String> summary = Lists.newArrayList();
                                    if (Objects.nonNull(resourceSubjectTeacherNumber) && resourceSubjectTeacherNumber > 0) {
                                        part.put("platform_1", true);
                                        summary.add("学校共计合作闻道资源平台的学科教师共计" + resourceSubjectTeacherNumber + "位");
                                    }
                                    if (Objects.nonNull(resourceSecretTeacherNumber) && resourceSecretTeacherNumber > 0) {
                                        part.put("platform_1", true);
                                        summary.add("试卷保密员共计" + resourceSecretTeacherNumber + "位");
                                    }
                                    if (!CollectionUtils.isEmpty(summary)) {
                                        part.put("platform_summary", summary.stream().collect(Collectors.joining("，", "本月，", "。")));
                                    } else {
                                        part.put("platform_summary", "本月暂无数据。");
                                    }


                                    Integer ssktAndMixSubjectTeacherNumber = po.getSsktAndMixSubjectTeacherNumber();
                                    if (Objects.nonNull(ssktAndMixSubjectTeacherNumber) && ssktAndMixSubjectTeacherNumber > 0) {
                                        part.put("platform_2", true);
                                        part.put("platform_title2", "第二部分 生生课堂班级应用分析");
                                        part.put("nested", createNestedDocument());
                                    }

                                    return part;
                                }
                           )
                           .orElse(Collections.emptyMap())
        );
        return map;
    }

    private static RenderData createNestedDocument() {
        DocumentRenderData documentRenderData = Documents.of().addParagraph(Paragraphs.of("history").create()).create();
        try (XWPFTemplate xwpfTemplate = XWPFTemplate.create(documentRenderData)) {
            return Includes.ofStream(PoitlIOUtils.docToInputStream(xwpfTemplate.getXWPFDocument())).create();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
